|
|
|
|
|
|
|
The core Win32 API functions do not use variants. However, the OLE subsystem does support variants, and those variants are fully compatible with Visual Basic. |
|
|
|
|
|
|
|
|
When you use variants in Visual Basic, you see them as a variable like any other. But DLL functions see variants as a 16-byte VARIANT structure. |
|
|
|
|
|
|
|
|
Unlike user-defined types, you can pass variants by value using the ByVal keyword. This is because Visual Basic knows how to make a copy of a variant. |
|
|
|
|
|
|
|
|
Variants appear in the C documentation as VARIANT or VARIANTARG structures. |
|
|
|
|
|
|
|
|
You've already seen the secret to handling arrays with Win32 API functions. Remember how a C language string is nothing more than a buffer of characters with a NULL termination, and that a pointer to a string is just a pointer to the first character in the string? API functions use this approach with any type of array. For example, an integer array will usually appear in a C declaration as LPINT or PINT. |
|
|
|
|
|
|
|
|
For example, let's say you have an API function that expects an array of five shorts. In Visual Basic, you might define the array as follows: |
|
|
|
|
|
|
|
|
Dim myArray(4) As Integer |
|
|
|
|
|
|
|
|
The API function expects a pointer to the array, which means a pointer to the first element in the array. How can you obtain a pointer to the first element of the myArray array? There are two ways, as you saw in Tutorial 5: |
|
|
|
|
|
|
|
|
Declare the parameter As Integer and pass the first element of the array as follows: |
|
|
|
 |
|
|
|
|
Declare Function MyFunction Lib "mydll.dll" (ArrayPointer As Integer) As Long
Call MyFunction(myArray(O)) |
|
|
|
|
|
|
|
|
Declare the parameter ByVal As Long and pass the address of the first element obtained using the VarPtr operator as follows: |
|
|
|
 |
|
|
|
|
Declare Function MyFunction Lib "mydll.dll" (ByVal ArrayPointer As Long) _
As Long
Call MyFunction(VarPtr(myArray(0))) |
|
|
|
|
|
|
|
|
The first option is the one most often used with API functions. |
|
|
|
|
|